<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\ConfigValueModel;
use think\Db;

/**
 * 房源管理
 *
 * @icon fa fa-circle-o
 */
class House extends Backend
{

    /**
     * House模型对象
     * @var \app\admin\model\House
     */
    protected $model = null;
    protected $dataLimitField = 'user_id';
    protected $dataLimit = true;
    protected $dataLimitFieldAutoFill = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\House;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("houseTypeList", ConfigValueModel::where('cid',5)->column('option_name'));
        // 付款方式
        $this->view->assign("paymentList", ConfigValueModel::where('cid',7)->column('option_name'));
        // 朝向
        $this->view->assign("faceList", ConfigValueModel::where('cid',9)->column('option_name'));
        // 出租类型
        $this->view->assign("rentTypeList", ConfigValueModel::where('cid',6)->column('option_name'));
        // 装修类型
        $this->view->assign("decorationTypeList", ConfigValueModel::where('cid',14)->column('option_name'));
         // 入住类型
        $this->view->assign("checkInList", ConfigValueModel::where('cid',13)->column('option_name'));
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
     
     public function copy($ids = null)
    {
        // 获取原始数据
        $original = $this->model->get($ids)->toArray();
        if (!$original) {
            $this->error(__('No Results were found'));
            return; // 明确返回，避免继续执行
        }
        
        $result = false;
        
        try {
            // 开启事务
            Db::startTrans();
            
            // 复制数据（转换为数组并移除可能存在的ID）
            $data = $original;
            unset($data['id']); // 移除主键，避免重复
            unset($data['create_time']);
            unset($data['update_time']);
            unset($data['delete_time']);
            unset($data['status_text']);
            unset($data['type_text']);
            unset($data['create_time_text']);
            unset($data['update_time_text']);
            unset($data['delete_time_text']);
            
            // 保存新记录
            $result = $this->model->create($data);
            
            // 提交事务
            Db::commit();
            
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error(__('Copy failed: ') . $e->getMessage());
            return; // 异常时返回
        }
        
        // 检查操作结果
        if (!$result) {
            $this->error(__('Copy failed'));
            return;
        }
        
        $this->success(__('Copy successful'));
    }
}
